package com.raos.example.swagger.controller;

import com.raos.example.swagger.model.SysUser;
import com.raos.example.swagger.response.R;
import com.raos.example.swagger.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

/**
 * TestController
 *
 * @author raos
 * @email 991207823@qq.com
 * @date 2022/6/21 20:21
 */
@Slf4j
@Api(tags = "Swagger测试接口")
@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private UserService userService;

    private Random random = new Random();

    @ApiOperation(value = "测试接口1")
    @GetMapping("/say")
    public R<String> say() {
        String hello = "Hello, Welcome To Swagger!";
        log.info(hello);
        return R.ok(hello);
    }

    @ApiOperation(value = "list接口")
    @GetMapping("/list")
    public R<List<SysUser>> list() {
        return R.ok(userService.queryAll());
    }

    @ApiOperation(value = "保存接口")
    @PostMapping("/save")
    public R<Integer> save() {
        SysUser sysUser = new SysUser();
        int nextLong = random.nextInt(20);
        sysUser.setUserId(nextLong);
        sysUser.setUsername(nextLong + "");
        sysUser.setPassword(sysUser.getUsername());
        sysUser.setPhone("155833012xx");
//        int j = 1/0;

        int i = userService.save(sysUser);
        return i > 0 ? R.ok(i) : R.error();
    }

    @ApiOperation(value = "删除接口")
    @PostMapping("/delete/{userId}")
    public R<Integer> delete(@PathVariable Long userId) {
        int i = userService.delete(userId);
        return i > 0 ? R.ok() : R.error();
    }

}
